package com.example.dou2204;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;

public class MyView extends View {
    Paint paint;//绘制的工具---对应着实际画画中的画笔
    int color = Color.RED;
    int time;//目的显示倒计时中的时间
    int angle;//目的显示时间外的圆环的具体弧度
    public MyView(Context context) {
        super(context);
        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        color = array.getInt(R.styleable.MyView_my_color, color);
        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(2);
        paint.setTextSize(30);
        paint.setStyle(Paint.Style.STROKE);//空心的    还有一个实心的
        paint.setAntiAlias(true);//抗锯齿化---让图形更加圆润
        //paint.setTextSize();//经常在设置文字的时候使用
    }

    //canvas:绘制的地方---对应着实际画画中的画布--纸
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      /*  canvas.drawCircle(100,100,90,paint);
        paint.setStrokeWidth(2);
        paint.setTextSize(30);
        canvas.drawText("2204真厉害",20,60,paint);
        RectF f = new RectF(10, 210, 110, 310);//这是一个正方形，确定了一个充满这个正方形的圆
        canvas.drawArc(f,0,60,false,paint);*/
        //先画圆弧，再画文字
        RectF f = new RectF(10, 10, 110, 110);
        canvas.drawArc(f,0,angle,false,paint);
        canvas.drawText(time+"",60,40,paint);
    }
    public void start(int i){
        time = i;
        //添加动画效果
        {
            ValueAnimator animator = ValueAnimator.ofInt(i, 0);
            animator.setDuration(i*1000);//时长
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    time = (int) animation.getAnimatedValue();
                    invalidate();//重新绘制图形
                    if(time==0){
                        ToastUtils.showLong("我该跳转到主页面中了");
                    }
                }
            });
            animator.start();
        }
        {
            ValueAnimator animator = ValueAnimator.ofInt(360, 0);
            animator.setDuration(i*1000);//时长
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                    angle = (int) animation.getAnimatedValue();
                    invalidate();//重新绘制图形
                }
            });
            animator.start();
        }
    }
    //想要跳转自定义的view,需要重写这个方法
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int mode1 = MeasureSpec.getMode(heightMeasureSpec);

        int size = MeasureSpec.getSize(widthMeasureSpec);
        int size1 = MeasureSpec.getSize(heightMeasureSpec);
        if(mode==MeasureSpec.AT_MOST){//当使用wrap_时
            size = 120;
        }
        if(mode1==MeasureSpec.AT_MOST){
            size1 = 120;
        }
        setMeasuredDimension(size,size1);
    }
}
